<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import Header from '../components/Header.vue'
import { getCourseDetailed } from '@/api/course'

const route = useRoute()
const router = useRouter()
const courseId = route.params.id
const courseName = ref('')
const courseDescription = ref('')
const chapters = ref([])
const activeChapters = ref([])

const fetchCourseDetailed = async () => {
  const res = await getCourseDetailed(courseId)
  if (res.code === 0 || res.code === 200) {
    courseName.value = res.data.courseName
    courseDescription.value = res.data.courseDescription || '本课程包含多个章节，助您系统掌握知识点。'
    chapters.value = res.data.chapters || []
    activeChapters.value = chapters.value.map(c => c.chapterId)
  }
}

function playVideo(video) {
  router.push({
    name: 'video-study',
    params: { id: courseId },
    query: { videoId: video.videoId }
  })
}

onMounted(() => {
  fetchCourseDetailed()
})
</script>

<template>
  <div class="course-detail-student-container">
    <Header />
    <div class="course-detail-background">
      <div class="course-detail-student-content">
        <div class="course-header">
          <h1 class="course-title">{{ courseName }}</h1>
          <p class="course-description">{{ courseDescription }}</p>
        </div>
        
        <div class="course-chapters">
          <h2 class="chapters-title">课程章节</h2>
          
          <el-collapse v-model="activeChapters" class="custom-collapse">
            <el-collapse-item
              v-for="(chapter, cIdx) in chapters"
              :key="chapter.chapterId"
              :title="'第' + (cIdx + 1) + '章 ' + chapter.chapterName"
            >
              <div v-if="chapter.videos && chapter.videos.length">
                <ul class="video-list">
                  <li v-for="(video, vIdx) in chapter.videos" :key="video.videoId" class="video-item">
                    <div class="video-item-left">
                      <span class="video-index">{{ cIdx + 1 }}-{{ vIdx + 1 }}</span>
                      <span class="video-name">{{ video.videoName }}</span>
                    </div>
                    <el-button
                      v-if="video.videoUrl"
                      type="primary"
                      class="play-button"
                      size="small"
                      @click="playVideo(video)"
                    >
                      播放视频
                    </el-button>
                  </li>
                </ul>
              </div>
              <div v-else class="empty-videos">
                <span>暂无视频内容</span>
              </div>
            </el-collapse-item>
          </el-collapse>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.course-detail-student-container {
  min-height: 100vh;
  font-family: 'Inter', 'Source Sans Pro', 'Microsoft YaHei', sans-serif;
}

.course-detail-background {
  background: linear-gradient(135deg, #f5f7fa 0%, #e4ebf5 100%);
  padding: 40px 20px 60px;
  min-height: calc(100vh - 60px);
}

.course-detail-student-content {
  max-width: 960px;
  margin: 0 auto;
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  padding: 0;
  overflow: hidden;
}

.course-header {
  background: linear-gradient(135deg, #2c5dff 0%, #0097ff 100%);
  color: white;
  padding: 40px 40px 50px;
  position: relative;
}

.course-header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 30px;
  background: white;
  border-radius: 30px 30px 0 0;
}

.course-title {
  font-size: 2.2rem;
  font-weight: 700;
  margin: 0 0 16px;
  letter-spacing: 0.5px;
  line-height: 1.3;
}

.course-description {
  font-size: 1.1rem;
  opacity: 0.9;
  margin: 0;
  line-height: 1.5;
  max-width: 800px;
}

.course-chapters {
  padding: 20px 40px 40px;
}

.chapters-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: #333;
  margin: 0 0 24px;
  position: relative;
  padding-left: 15px;
}

.chapters-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 5px;
  height: 24px;
  background: linear-gradient(180deg, #2c5dff 0%, #0097ff 100%);
  border-radius: 3px;
}

.custom-collapse {
  border: none;
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.05);
  border-radius: 12px;
  overflow: hidden;
}

:deep(.el-collapse-item__header) {
  font-size: 1.15rem;
  font-weight: 600;
  padding: 16px 20px;
  background-color: #f8f9fd;
  border-bottom: 1px solid #e9ecf8;
  transition: all 0.3s;
}

:deep(.el-collapse-item__header:hover) {
  background-color: #eef2ff;
}

:deep(.el-collapse-item__content) {
  padding: 5px 0;
}

.video-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.video-item {
  padding: 15px 20px;
  border-bottom: 1px solid #f0f2fa;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: background-color 0.2s;
}

.video-item:hover {
  background-color: #f9faff;
}

.video-item:last-child {
  border-bottom: none;
}

.video-item-left {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
}

.video-index {
  color: #2c5dff;
  font-weight: 600;
  font-size: 15px;
  background: rgba(44, 93, 255, 0.1);
  padding: 4px 8px;
  border-radius: 6px;
  min-width: 42px;
  text-align: center;
}

.video-name {
  font-size: 16px;
  color: #333;
  flex: 1;
}

.play-button {
  background: linear-gradient(135deg, #2c5dff 0%, #0097ff 100%);
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  transition: all 0.3s;
}

.play-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(44, 93, 255, 0.25);
}

.empty-videos {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 30px 0;
  color: #94a3b8;
  gap: 8px;
  font-size: 15px;
}

@media (max-width: 768px) {
  .course-header {
    padding: 30px 25px 40px;
  }
  
  .course-title {
    font-size: 1.8rem;
  }
  
  .course-description {
    font-size: 1rem;
  }
  
  .course-chapters {
    padding: 20px 20px 30px;
  }
  
  .video-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }
  
  .play-button {
    align-self: flex-end;
  }
}
</style> 